package com.shajj.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.TitleTag;
import org.htmlparser.util.NodeList;

import com.google.appengine.api.datastore.Text;

@SuppressWarnings("serial")
public class ShajjcdnServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger("shajj");

	private Long id = 1L;

	private String target = "http://www.caoegg.cn/view/";

	Parser parser = new Parser();
	NodeFilter filter = new NodeClassFilter(TitleTag.class);

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String newId = req.getParameter("startId");
		if (newId != null) {
			id = Long.parseLong(newId);
		} else {
			id = getCurrentId();
		}
		resp.setContentType("text/plain;charset=utf-8");
		if (id <= 0) {
			resp.getWriter().write("error: id cannot be zero.");
			resp.flushBuffer();
			return;
		}
		while (id > 0) {
			try {
				Message message = DataManager.getInstance().get(id);
				if (message != null) {
					break;
				}
				URL url = new URL(target + id);
				BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
				String line;
				StringBuilder sb = new StringBuilder();
				while ((line = reader.readLine()) != null) {
					sb.append(line);
				}
				reader.close();
				parser.setInputHTML(sb.toString());
				NodeList list = parser.extractAllNodesThatMatch(filter);
				if (list != null && list.size() > 0) {
					String content = ((TitleTag) list.elementAt(0)).getStringText();
					if (content.startsWith("草蛋")) {
						continue;
					}
					Text text = new Text(content);
					DataManager.getInstance().save(new Message(id, new Date(), text));
					log.warning(id + " saved.");
				}
				id--;
			} catch (Exception e) {
				log.warning("抓取数据错误：" + e.getMessage());
			}
		}
		resp.getWriter().write("finished at id:" + id);
		resp.flushBuffer();

	}

	private Long getCurrentId() {
		try {
			URL url = new URL("http://www.caoegg.cn");
			BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
			String line;
			StringBuilder sb = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();
			Pattern pattern = Pattern.compile("/view/(\\d+)", Pattern.DOTALL);
			Matcher matcher = pattern.matcher(sb.toString());
			if (matcher.find()) {
				return Long.parseLong(matcher.group(1));
			}
		} catch (Exception e) {
			log.warning("取当前最新ID失败:" + e.getMessage());
		}

		return 0L;
	}

}
